/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file creates all tables and figures presented in the appendix of
our paper. Note, the "main_text_results.do" file must be run before 
running this do file. This .do file executes the following
STATA do files and r scripts located within the "code" folder:
	1. appendices/figureA1andA2.do
	2. appendices/figureA3.do
	3. appendices/figureA4andA5.do
	4. appendices/figureB1andB2.do
	5. appendices/figureB3.do
*/

*Define directories and set working directory
/* NOTE - To replicate our results, you need to change the root directory address in the next line */
global dr_root = "\replication materials"
global dr_code = "${dr_root}\code"
global dr_data = "${dr_root}\data"
global dr_output = "${dr_root}\outputs"
global dr_output_main = "${dr_root}\outputs\main_text"
global dr_output_app = "${dr_root}\outputs\appendices"
global dr_output_log = "${dr_root}\outputs\logs"
global dr_temp = "${dr_root}\data\intermediate"
cd "${dr_root}"

*Open log file
	log using "${dr_output_log}/appendices_results", replace

*Appendix A materials
	/* Run do file creating Figures A1 and A2 */
	do "${dr_code}/appendices/figureA1andA2.do"

	/* Run do file creating Figure A3 */
	do "${dr_code}/appendices/figureA3.do"

	/* Run do file creating figures A4 and A5 */
	do "${dr_code}/appendices/figureA4andA5.do"

*Appendix B materials
	/* Run do file creating figures B1 and B2 */
	do "${dr_code}/appendices/figureB1andB2.do"

	/* Run do file creating Figure B3 */
	do "${dr_code}/appendices/figureB3.do"
	
	/* Run r script performing bacon decomposition and 
		then output Table B.1 and Figure B.4 */
	rscript using "${dr_code}/appendices/bacon_decomp.R", ///
		args("${dr_root}") rversion(3.6)

*Appendix C materials
	/* Table C.1 contains the estimated average treatment effects for both 
		technology changes produced by TWFE and the Callaway & Sant'Anna 
		estimator in levels. The "figure3.do" file, executed by the 
		"main_text_results.do" file, creates a dta file for each
		transition containing the ATT estimates in levels */
		*Load flood to cp/lepa ATT dta file
			use "${dr_output_app}/floodtocplepa_att_twfecs.dta", clear
		*Append the ATT estimates for cp to LEPA transition
			append using "${dr_output_app}/cptolepa_att_twfecs.dta"
		*Drop scaled estimate variables
			drop scaled*
		*Save 
			export delimited using "${dr_output_app}/tableC1.csv", replace			
		
	/* Tables C.2 and C.3 contain the estimated cohort treatment effects for
		the change from flood to traditional center pivot or LEPA irrigation and from
		tranditional center pivot to LEPA, respectively. The estimates for  
		both tables are produced using the Callaway & Sant'Anna 
		estimator by the "cs_estimates.R" rscript run by the 
		"main_text_results.do" file. */
		/* Table C.2 -Load in the sheet containing the cohort treatment effects 
			from the flood to cp or lepa excel file containing average, cohort, 
			and dynamic effects. */			
			import excel "${dr_temp}/cs_floodtocporlepa_91_15_nyt.xlsx", ///
				sheet("cs_group") firstrow clear
			*Create variable showing which transition it is
			gen transition = "flood to cp/LEPA"
			*Save as tableC2.csv
			export delimited using "${dr_output_app}/tableC2.csv", replace
		/* Table C.3 - Load in the sheet containing the cohort treatment effects
			from the flood to cp or lepa excel file containing average, cohort, 
			and dynamic effects. */			
			import excel "${dr_temp}/cs_cptolepa.xlsx", ///
				sheet("cs_group") firstrow clear
			*Create variable showing which transition it is
			gen transition = "cp to LEPA"
			*Save as tableC3.csv
			export delimited using "${dr_output_app}/tableC3.csv", replace

	/* Table C.4 - Run r script estimating effects of transitioning from 
		flood irrigation to traditional center pivot and from flood irrigation
		to LEPA separately. */
		rscript using "${dr_code}/appendices/tableC4.R", ///
			args("${dr_root}") rversion(3.6)
			
	/* Table C.5 - Run r script estimating effects of both transitions on the 
		percent of water right groups' irrigated acreage planted to five
		crops using Callaway and Sant'Anna estimator. The crops are: alfalfa,
		corn, sorghum, soybeans, and wheat. */
		rscript using "${dr_code}/appendices/cs_estimates_frac_crop.R", ///
			args("${dr_root}") rversion(3.6)		
		
	/* Tables C.6 and C.7 contain the estimated dynamic treatment effects for
		the change from flood to traditional center pivot or LEPA irrigation and from
		tranditional center pivot to LEPA, respectively. The estimates for  
		both tables are produced using the Callaway & Sant'Anna 
		estimator by the "cs_estimates.R" rscript run by the 
		"main_text_results.do" file. */
		/* Table C.6 - Load in the sheet containing the dynamic treatment effects 
			from the flood to cp or lepa excel file containing average, cohort, 
			and dynamic effects. */			
			import excel "${dr_temp}/cs_floodtocporlepa_91_15_nyt.xlsx", ///
				sheet("cs_event") firstrow clear
			*Create variable showing which transition it is
			gen transition = "flood to cp/LEPA"
			*Save as tableC6.csv
			export delimited using "${dr_output_app}/tableC6.csv", replace
		/* Table C.7 - Load in the sheet containing the dynamic treatment effects
			from the flood to cp or lepa excel file containing average, cohort, 
			and dynamic effects. */			
			import excel "${dr_temp}/cs_cptolepa.xlsx", ///
				sheet("cs_event") firstrow clear
			*Create variable showing which transition it is
			gen transition = "cp to LEPA"
			*Save as tableC7.csv
			export delimited using "${dr_output_app}/tableC7.csv", replace

*Appendix D materials
	/* Table D.1 & D.2 - Run r script performing the parallel trends pre-test
		for both transitions and store the output for tables D.1 and D.2. */
		rscript using "${dr_code}/appendices/tableD1andD2.R", ///
			args("${dr_root}") rversion(3.6)
			
	/* Table D.3 - Run the do file which produces TWFE ATT estimates for both 
		transitions during the period when the pre-test passes (1996-2005). Then
		run the r script estimating the ATT for the same period using the 
		Callaway and Sant'Anna estimator. */
		do "${dr_code}/appendices/twfe_att_96_05.do"
		rscript using "${dr_code}/appendices/cs_estimates_96_05.R", ///
			args("${dr_root}") rversion(3.6)
		*Load each output, save first three as .dta, then combine them and export
		import excel "${dr_temp}/twfe.xlsx", ///
				sheet("floodtocplepa_96_05_nyt") firstrow clear
				gen transition = "flood to center pivot or LEPA"
				gen est = "twfe"
				replace lb_estimate = estimate - 1.96*se_estimate
				replace ub_estimate = estimate + 1.96*se_estimate
				replace scaled_estimate = 100*estimate/panel_mean_dep_var
				replace scaled_lb = 100*lb_estimate/panel_mean_dep_var
				replace scaled_ub = 100*ub_estimate/panel_mean_dep_var
				keep dep_var est transition scaled_estimate scaled_lb ///
					scaled_ub estimate se_estimate lb_estimate ub_estimate
				save "${dr_temp}/twfe_flood_cplepa_96_05.dta", replace
		import excel "${dr_temp}/twfe.xlsx", ///
				sheet("cplepa_96_05") firstrow clear
				gen transition = "center pivot to LEPA"
				gen est = "twfe"
				replace lb_estimate = estimate - 1.96*se_estimate
				replace ub_estimate = estimate + 1.96*se_estimate
				replace scaled_estimate = 100*estimate/panel_mean_dep_var
				replace scaled_lb = 100*lb_estimate/panel_mean_dep_var
				replace scaled_ub = 100*ub_estimate/panel_mean_dep_var
				keep dep_var est transition scaled_estimate scaled_lb ///
					scaled_ub estimate se_estimate lb_estimate ub_estimate
				save "${dr_temp}/twfe_cp_lepa_96_05.dta", replace
		import delimited using "${dr_temp}/cs_estimates_att_96_05.csv", clear 
				gen est = "cs"
				gen scaled_estimate = 100*estimate/panel_mean_dep_var
				gen scaled_lb = 100*ci_95_lb/panel_mean_dep_var
				gen scaled_ub = 100*ci_95_ub/panel_mean_dep_var
				rename (ci_95_lb ci_95_ub) (lb_estimate ub_estimate)
				keep dep_var est transition scaled_estimate scaled_lb ///
					scaled_ub estimate se_estimate lb_estimate ub_estimate
		*Add TWFE estimates
		append using "${dr_temp}/twfe_cp_lepa_96_05.dta"
		append using "${dr_temp}/twfe_flood_cplepa_96_05.dta"
		*Save
		export delimited using "${dr_output_app}/tableD3.csv", replace

	/* Figures D.1 and D.2 - Run the do file which graphs the Callaway and 
		Sant'Anna estimator dynamic treatment effect results for both transitions 
		during the period when the pre-test passes (1996-2005). The estimates 
		are produced earlier in this do file when the 
		appendices/cs_estimates_96_05.R" script is called. */	
		do "${dr_code}/appendices/figureD1andD2.do"

	/* Table D.4, D.5, D.6, D.7, D.8, and D.9 - Run the r script which 
		estimates balanced dynamic treatment effects for both transitions 
		using the Callaway and Sant'Anna estimator and the years 1996-2005.
		The results are output as two excel files. The values for each table 
		can be recovered by selecting the specific dependent variable of 
		interest. For example, Table D.4 contains the results for acre-feet
		of withdrawals. */	
		rscript using "${dr_code}/appendices/cs_estimates_bal_dyn.R", ///
			args("${dr_root}") rversion(3.6)
			
	/* Table D.10 and Figure D.3 - Run do file which estimates average and dynamic
		treatment effects for both transitions using the de Chaisemartin &
		D'Haultfoeuille	(2021) estimator, "app_cd.do".
		Then run a separate do file, "figureD3.do", which combines
		these results with the TWFE and Callaway Sant'Anna (2020) estimators' 
		results (both produced by running the "main_text_results.do" file). 
		The "figureD3.do" file outputs a spreadsheet containing the ATT
		estimates for all three	estimators in levels for both transitions,
		the information in Table D.10. It also creates Figure D.3. */
		do "${dr_code}/appendices/app_cd.do"
		do "${dr_code}/appendices/figureD3.do"

	/* Figures D.4 and D.5 - This do file combines the dynamic treatment
		effects for the de Chaisemartin & D'Haultfoeuille (2021) estimator with
		the results from the TWFE and Callaway Sant'Anna (2020) estimators 
		produced by	running the "main_text_results.do" file. It then graphs
		the dynamic effects for the flood to center pivot or LEPA transition 
		(Figure D.4) and the transition from center pivot to LEPA (Figure D.5). */
		do "${dr_code}/appendices/figureD4andD5.do"

	/* Table D.11 - This is created by manually updating the values in the excel 
		spreadsheet, "outputs\appendices\tableD11.xlsx", for the TWFE and
		CS estimates and standard errors from Table C.1 */
		
*Close log
log close